home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxshade.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  10.1 KB  |  361 lines

  1. /* Copyright (C) 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxshade.c,v 1.4 2000/09/19 19:00:40 lpd Exp $ */
  20. /* Shading rendering support */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsrect.h"
  25. #include "gxcspace.h"
  26. #include "gscie.h"        /* requires gscspace.h */
  27. #include "gxdevcli.h"
  28. #include "gxistate.h"
  29. #include "gxdht.h"        /* for computing # of different colors */
  30. #include "gxpaint.h"
  31. #include "gxshade.h"
  32.  
  33. /* Define a maximum smoothness value. */
  34. /* smoothness > 0.2 produces severely blocky output. */
  35. #define MAX_SMOOTHNESS 0.2
  36.  
  37. /* ================ Packed coordinate streams ================ */
  38.  
  39. /* Forward references */
  40. private int cs_next_packed_value(P3(shade_coord_stream_t *, int, uint *));
  41. private int cs_next_array_value(P3(shade_coord_stream_t *, int, uint *));
  42. private int cs_next_packed_decoded(P4(shade_coord_stream_t *, int,
  43.                       const float[2], float *));
  44. private int cs_next_array_decoded(P4(shade_coord_stream_t *, int,
  45.                      const float[2], float *));
  46.  
  47. /* Initialize a packed value stream. */
  48. void
  49. shade_next_init(shade_coord_stream_t * cs,
  50.         const gs_shading_mesh_params_t * params,
  51.         const gs_imager_state * pis)
  52. {
  53.     cs->params = params;
  54.     cs->pctm = &pis->ctm;
  55.     if (data_source_is_stream(params->DataSource)) {
  56.     /*
  57.      * Reset the data stream iff it is reusable -- either a reusable
  58.      * file or a reusable string.
  59.      */
  60.     stream *s = cs->s = params->DataSource.data.strm;
  61.  
  62.     if ((s->file != 0 && s->file_limit != max_long) ||
  63.         (s->file == 0 && s->strm == 0)
  64.         )
  65.         sreset(s);
  66.     } else {
  67.     sread_string(&cs->ds, params->DataSource.data.str.data,
  68.              params->DataSource.data.str.size);
  69.     cs->s = &cs->ds;
  70.     }
  71.     if (data_source_is_array(params->DataSource)) {
  72.     cs->get_value = cs_next_array_value;
  73.     cs->get_decoded = cs_next_array_decoded;
  74.     } else {
  75.     cs->get_value = cs_next_packed_value;
  76.     cs->get_decoded = cs_next_packed_decoded;
  77.     }
  78.     cs->left = 0;
  79. }
  80.  
  81. /* Get the next (integer) value from a packed value stream. */
  82. /* 1 <= num_bits <= sizeof(uint) * 8. */
  83. private int
  84. cs_next_packed_value(shade_coord_stream_t * cs, int num_bits, uint * pvalue)
  85. {
  86.     uint bits = cs->bits;
  87.     int left = cs->left;
  88.  
  89.     if (left >= num_bits) {
  90.     /* We can satisfy this request with the current buffered bits. */
  91.     cs->left = left -= num_bits;
  92.     *pvalue = (bits >> left) & ((1 << num_bits) - 1);
  93.     } else {
  94.     /* We need more bits. */
  95.     int needed = num_bits - left;
  96.     uint value = bits & ((1 << left) - 1);    /* all the remaining bits */
  97.  
  98.     for (; needed >= 8; needed -= 8) {
  99.         int b = sgetc(cs->s);
  100.  
  101.         if (b < 0)
  102.         return_error(gs_error_rangecheck);
  103.         value = (value << 8) + b;
  104.     }
  105.     if (needed == 0) {
  106.         cs->left = 0;
  107.         *pvalue = value;
  108.     } else {
  109.         int b = sgetc(cs->s);
  110.  
  111.         if (b < 0)
  112.         return_error(gs_error_rangecheck);
  113.         cs->bits = b;
  114.         cs->left = left = 8 - needed;
  115.         *pvalue = (value << needed) + (b >> left);
  116.     }
  117.     }
  118.     return 0;
  119. }
  120.  
  121. /* Get the next (integer) value from an unpacked array. */
  122. private int
  123. cs_next_array_value(shade_coord_stream_t * cs, int num_bits, uint * pvalue)
  124. {
  125.     float value;
  126.     uint read;
  127.  
  128.     if (sgets(cs->s, (byte *)&value, sizeof(float), &read) < 0 ||
  129.     read != sizeof(float) || value < 0 || value >= (1 << num_bits) ||
  130.     value != (int)value
  131.     )
  132.     return_error(gs_error_rangecheck);
  133.     *pvalue = (uint) value;
  134.     return 0;
  135. }
  136.  
  137. /* Get the next decoded floating point value. */
  138. private int
  139. cs_next_packed_decoded(shade_coord_stream_t * cs, int num_bits,
  140.                const float decode[2], float *pvalue)
  141. {
  142.     uint value;
  143.     int code = cs->get_value(cs, num_bits, &value);
  144. #if ARCH_CAN_SHIFT_FULL_LONG
  145.     double max_value = (double)(uint) ((1 << num_bits) - 1);
  146. #else
  147.     double max_value = (double)(uint)
  148.     (num_bits == sizeof(uint) * 8 ? ~0 : ((1 << num_bits) - 1));
  149. #endif
  150.  
  151.     if (code < 0)
  152.     return code;
  153.     *pvalue =
  154.     (decode == 0 ? value / max_value :
  155.      decode[0] + value * (decode[1] - decode[0]) / max_value);
  156.     return 0;
  157. }
  158.  
  159. /* Get the next floating point value from an array, without decoding. */
  160. private int
  161. cs_next_array_decoded(shade_coord_stream_t * cs, int num_bits,
  162.               const float decode[2], float *pvalue)
  163. {
  164.     float value;
  165.     uint read;
  166.  
  167.     if (sgets(cs->s, (byte *)&value, sizeof(float), &read) < 0 ||
  168.     read != sizeof(float)
  169.     )
  170.     return_error(gs_error_rangecheck);
  171.     *pvalue = value;
  172.     return 0;
  173. }
  174.  
  175. /* Get the next flag value. */
  176. /* Note that this always starts a new data byte. */
  177. int
  178. shade_next_flag(shade_coord_stream_t * cs, int BitsPerFlag)
  179. {
  180.     uint flag;
  181.     int code;
  182.  
  183.     cs->left = 0;        /* start a new byte if packed */
  184.     code = cs->get_value(cs, BitsPerFlag, &flag);
  185.     return (code < 0 ? code : flag);
  186. }
  187.  
  188. /* Get one or more coordinate pairs. */
  189. int
  190. shade_next_coords(shade_coord_stream_t * cs, gs_fixed_point * ppt,
  191.           int num_points)
  192. {
  193.     int num_bits = cs->params->BitsPerCoordinate;
  194.     const float *decode = cs->params->Decode;
  195.     int code = 0;
  196.     int i;
  197.  
  198.     for (i = 0; i < num_points; ++i) {
  199.     float x, y;
  200.  
  201.     if ((code = cs->get_decoded(cs, num_bits, decode, &x)) < 0 ||
  202.         (code = cs->get_decoded(cs, num_bits, decode + 2, &y)) < 0 ||
  203.         (code = gs_point_transform2fixed(cs->pctm, x, y, &ppt[i])) < 0
  204.         )
  205.         break;
  206.     }
  207.     return code;
  208. }
  209.  
  210. /* Get a color.  Currently all this does is look up Indexed colors. */
  211. int
  212. shade_next_color(shade_coord_stream_t * cs, float *pc)
  213. {
  214.     const float *decode = cs->params->Decode + 4;    /* skip coord decode */
  215.     const gs_color_space *pcs = cs->params->ColorSpace;
  216.     gs_color_space_index index = gs_color_space_get_index(pcs);
  217.     int num_bits = cs->params->BitsPerComponent;
  218.  
  219.     if (index == gs_color_space_index_Indexed) {
  220.     uint i;
  221.     int code = cs->get_value(cs, num_bits, &i);
  222.  
  223.     if (code < 0)
  224.         return code;
  225.     /****** DO INDEXED LOOKUP TO pc[] ******/
  226.     } else {
  227.     int i, code;
  228.     int ncomp = gs_color_space_num_components(pcs);
  229.  
  230.     for (i = 0; i < ncomp; ++i)
  231.         if ((code = cs->get_decoded(cs, num_bits, decode + i * 2, &pc[i])) < 0)
  232.         return code;
  233.     }
  234.     return 0;
  235. }
  236.  
  237. /* Get the next vertex for a mesh element. */
  238. int
  239. shade_next_vertex(shade_coord_stream_t * cs, mesh_vertex_t * vertex)
  240. {
  241.     int code = shade_next_coords(cs, &vertex->p, 1);
  242.  
  243.     if (code >= 0)
  244.     code = shade_next_color(cs, vertex->cc);
  245.     return code;
  246. }
  247.  
  248. /* ================ Shading rendering ================ */
  249.  
  250. /* Initialize the common parts of the recursion state. */
  251. void
  252. shade_init_fill_state(shading_fill_state_t * pfs, const gs_shading_t * psh,
  253.               gx_device * dev, gs_imager_state * pis)
  254. {
  255.     const gs_color_space *pcs = psh->params.ColorSpace;
  256.     float max_error = min(pis->smoothness, MAX_SMOOTHNESS);
  257.     /*
  258.      * There's no point in trying to achieve smoothness beyond what
  259.      * the device can implement, i.e., the number of representable
  260.      * colors times the number of halftone levels.
  261.      */
  262.     long num_colors =
  263.     max(dev->color_info.max_gray, dev->color_info.max_color) + 1;
  264.     const gs_range *ranges = 0;
  265.     int ci;
  266.  
  267.     pfs->dev = dev;
  268.     pfs->pis = pis;
  269.     pfs->num_components = gs_color_space_num_components(pcs);
  270. top:
  271.     switch ( gs_color_space_get_index(pcs) )
  272.     {
  273.     case gs_color_space_index_Indexed:
  274.         pcs = gs_cspace_base_space(pcs);
  275.         goto top;
  276.     case gs_color_space_index_CIEDEFG:
  277.         ranges = pcs->params.defg->RangeDEFG.ranges;
  278.         break;
  279.     case gs_color_space_index_CIEDEF:
  280.         ranges = pcs->params.def->RangeDEF.ranges;
  281.         break;
  282.     case gs_color_space_index_CIEABC:
  283.         ranges = pcs->params.abc->RangeABC.ranges;
  284.         break;
  285.     case gs_color_space_index_CIEA:
  286.         ranges = &pcs->params.a->RangeA;
  287.         break;
  288.     default:
  289.         break;
  290.     }
  291.     if (num_colors <= 32) {
  292.     /****** WRONG FOR MULTI-PLANE HALFTONES ******/
  293.     num_colors *= pis->dev_ht->order.num_levels;
  294.     }
  295.     if (max_error < 1.0 / num_colors)
  296.     max_error = 1.0 / num_colors;
  297.     for (ci = 0; ci < pfs->num_components; ++ci)
  298.     pfs->cc_max_error[ci] =
  299.         (ranges == 0 ? max_error :
  300.          max_error * (ranges[ci].rmax - ranges[ci].rmin));
  301. }
  302.  
  303. /* Transform a bounding box into device space. */
  304. int
  305. shade_bbox_transform2fixed(const gs_rect * rect, const gs_imager_state * pis,
  306.                gs_fixed_rect * rfixed)
  307. {
  308.     gs_rect dev_rect;
  309.     int code = gs_bbox_transform(rect, &ctm_only(pis), &dev_rect);
  310.  
  311.     if (code >= 0) {
  312.     rfixed->p.x = float2fixed(dev_rect.p.x);
  313.     rfixed->p.y = float2fixed(dev_rect.p.y);
  314.     rfixed->q.x = float2fixed(dev_rect.q.x);
  315.     rfixed->q.y = float2fixed(dev_rect.q.y);
  316.     }
  317.     return code;
  318. }
  319.  
  320. /* Check whether 4 colors fall within the smoothness criterion. */
  321. bool
  322. shade_colors4_converge(const gs_client_color cc[4],
  323.                const shading_fill_state_t * pfs)
  324. {
  325.     int ci;
  326.  
  327.     for (ci = 0; ci < pfs->num_components; ++ci) {
  328.     float
  329.           c0 = cc[0].paint.values[ci], c1 = cc[1].paint.values[ci],
  330.           c2 = cc[2].paint.values[ci], c3 = cc[3].paint.values[ci];
  331.     float min01, max01, min23, max23;
  332.  
  333.     if (c0 < c1)
  334.         min01 = c0, max01 = c1;
  335.     else
  336.         min01 = c1, max01 = c0;
  337.     if (c2 < c3)
  338.         min23 = c2, max23 = c3;
  339.     else
  340.         min23 = c3, max23 = c2;
  341.     if (max(max01, max23) - min(min01, min23) > pfs->cc_max_error[ci])
  342.         return false;
  343.     }
  344.     return true;
  345. }
  346.  
  347. /* Fill one piece of a shading. */
  348. int
  349. shade_fill_path(const shading_fill_state_t * pfs, gx_path * ppath,
  350.         gx_device_color * pdevc)
  351. {
  352.     gx_fill_params params;
  353.  
  354.     params.rule = -1;        /* irrelevant */
  355.     params.adjust = pfs->pis->fill_adjust;
  356.     params.flatness = 0;    /* irrelevant */
  357.     params.fill_zero_width = false;
  358.     return (*dev_proc(pfs->dev, fill_path)) (pfs->dev, pfs->pis, ppath,
  359.                          ¶ms, pdevc, NULL);
  360. }
  361.